home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8961 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: news3.noc.netcom.net!silvaco!spock!mikep
  2. From: mikep@Silvaco.COM (Mike Potts)
  3. Newsgroups: comp.lang.c++
  4. Subject: Q: how to store class member function pointers in class B
  5. Date: 27 Feb 1996 17:02:10 GMT
  6. Organization: Silvaco Data Systems, Inc, Santa Clara, CA
  7. Message-ID: <4gvdei$rra@news.Silvaco.COM>
  8. X-Newsreader: TIN [version 1.2 PL2]
  9.  
  10. This is a dumb question from a relative newbie: can I store a pointer to
  11. a member function of one class in another class?  Specifically, in a gui
  12. application, I want to construct a button class which stores a pointer
  13. to a callback in its parent.  I'm currently trying something like:
  14.  
  15. class Button : public ProtoButton {
  16.   void (Panel::*function)(void);
  17.  
  18.   void inheritedCallback(void) {
  19.     this->*function();
  20.   }
  21.  
  22.   Button((Panel::*func)(void)) {
  23.     function = func;
  24.   }
  25. }
  26.  
  27. class Panel : public protoPanel {
  28.   friend class Button;
  29.  
  30.   void buttonCallback(void) {...}
  31.  
  32.   Panel(void) {
  33.     new Button(buttonCallback);
  34.   }
  35. }
  36.  
  37. but I get compile-time errors along the lines that there's a problem making
  38. a cast from (Button) this to (Panel) this, which I completely fail to make
  39. head nor tail of. If this doesn't make any sense I apologise; if there's a
  40. sensible way to do this I'd be grateful if someone could enlighten me!
  41.  
  42. Thanks
  43.  
  44. Mike
  45.  
  46.